package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.management.Query;
import javax.sql.DataSource;

/**
 *
 */
@PropertySource(value = "classpath:db.properties")
public class DaoConfig {
    //读取配置文件  读取的是配置文件的key
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.driver}")
    private String driver;

    @Bean("queryRunner")
    public QueryRunner crrateQueryRunner (DataSource dataSource) {

        QueryRunner queryRunner = new QueryRunner(dataSource);

        return queryRunner;
    }

    @Bean("DataSource")
    public DataSource createDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
